using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;

namespace Magiq.Sql.Support
{
    public class TypeInheritanceMapping
    {
        private readonly MetaType metaType;
        private MetaDataMember discriminator;

        public TypeInheritanceMapping(Type type, DataContext dataContext)
        {
            Type = type;
            metaType = dataContext.Mapping.GetMetaType(Type);
        }

        public PropertyInfo DiscriminatorProperty
        {
            get { return (PropertyInfo) Discriminator.Member; }
        }

        public bool IsInheritance
        {
            get { return Discriminator != null; }
        }

        private MetaDataMember Discriminator
        {
            get
            {
                if (discriminator == null)
                    discriminator = metaType.Discriminator;
                return discriminator;
            }
        }

        public object DiscriminatorCode
        {
            get { return metaType.InheritanceCode; }
        }

        public Type Type { get; private set; }
    }
}